package com.google.code.tretristest.network;

import junit.framework.TestCase;

import org.jivesoftware.smack.packet.Message;

import com.google.code.tretris.events.MoveEvent;
import com.google.code.tretris.events.UpdateBoardEvent;
import com.google.code.tretris.network.NetworkManager;
import com.google.code.tretris.network.events.TretrisMessage;
import com.google.code.tretris.network.game.GameActionListener;
import com.google.code.tretristest.network.mockconnection.MockedXMPPConnection;

public class TretrisPacketParsingTestCase extends TestCase{

	NetworkManager manager;
	MockListener lester;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		manager = NetworkManager.newInstance(new MockedXMPPConnection());
		lester = new MockListener();
		manager.addMoveListener(lester);
		manager.addRotationListener(lester);
		manager.addUpdateListener(lester);
	}
	
	public void testPacketparseMoveEvent() throws Exception {
		
		Message testPack = new Message();
		
		// Mocka ett move event
		testPack.setBody(TretrisMessage.MOVE_EVENT+"#"+1);
	
		assertFalse(lester.moved);
		manager.processPacket(testPack);
		assertTrue(lester.moved);
		
	}
	
	public void testPacketparseRotateEvent() throws Exception {
		
		Message testPack = new Message();
		
		// Mocka ett rotate event
		testPack.setBody(TretrisMessage.ROTATE_EVENT+"#"+1);
		
		assertFalse(lester.rotated);
		manager.processPacket(testPack);
		assertTrue(lester.rotated);
		
	}
	
	public void testPacketparseUpdateEvent() throws Exception {

		Message testPack = new Message();
		
		// Mocka ett update event
		testPack.setBody(TretrisMessage.UPDATEBOARD_EVENT+"#"
				+"1@test.nu;" +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0;" +
						"Z,4,3,0"
				+"/2@test.nu;" +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0;" +
						"Z,4,3,0"
				+"/3@test.nu;" +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0," +
						"0,0,0,0,0,0,0,0,0,0;" +
						"Z,4,3,0");
			
		assertFalse(lester.updated);
		manager.processPacket(testPack);
		assertTrue(lester.updated);
		
	}
	
	
	public class MockListener implements GameActionListener {

		public boolean moved = false;
		public void move(MoveEvent e) {
			moved = true;
		}

		public boolean rotated = false;
		public void rotate(MoveEvent e) {
			rotated = true;
		}

		public boolean updated = false;
		public void update(UpdateBoardEvent e) {
			updated = true;
		}
		
	}
	
}
